﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.SafetyManagementEntity.Model;
using KingRoad.EPM.Web.Areas.SafetyConstruction.ViewModels;
using KingRoad.EPM.Web.Areas.SafetyConstruction.SafetyConstructionFramework.Common;

namespace KingRoad.EPM.Web.Areas.SafetyConstruction.Controllers
{
    public class ScmSafetyConstructionDicController : BaseController
    {
        //
        // GET: /ScmSafetyConstructionDic/
        public ActionResult SafetyConstructionDicIndex()
        {
            return View();
        }

        public ActionResult SafetyConstructionDicList(string Scd_ID)
        {
            string sql = " scd_parentid='" + Scd_ID + "' ";
            ViewBag.PId = Scd_ID;
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            var dicList = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicForPage(ViewBag.pageNum, ViewBag.numPerPage, "Scd_Code", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new ScmSafetyconstructiondicViewModel { TheSafetyconstructiondicList = dicList });
        }
        /// <summary>
        /// 特种设备台账-列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.PId = Request.Form["PId"];
            ViewBag.iput_name = Request.Form["iput_name"];
            ViewBag.iput_code = Request.Form["iput_code"];

            string sql = " 1=1 ";
            if (ViewBag.iput_equipment != "")
                sql += " and Scd_Name like '%" + ViewBag.iput_name + "%' ";
            if (ViewBag.iput_model != "")
                sql += " and Scd_Code like '%" + ViewBag.iput_code + "%' ";
            if (ViewBag.Section != "")
                sql += " and Scd_ParentID= '" + ViewBag.PId + "' ";

            var dicList = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicForPage(ViewBag.pageNum, ViewBag.numPerPage, "Scd_Code", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("SafetyConstructionDicList", new ScmSafetyconstructiondicViewModel { TheSafetyconstructiondicList = dicList });
        }
        public ActionResult SafetyConstructionDicAdd(string pid)
        {
            ScmSafetyconstructiondicViewModel model = new ScmSafetyconstructiondicViewModel();
            ScmSafetyconstructiondicInfo info = new ScmSafetyconstructiondicInfo();
            info.Scd_ID = Guid.NewGuid().ToString();
            info.Scd_ParentID = pid;
            model.TheSafetyconstructiondicInfo = info;
            return View(model);
        }

        public JsonResult SafetyConstructionDicAddData(ScmSafetyconstructiondicViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                ScmSafetyconstructiondicUIRule.InsertEntity(vm.TheSafetyconstructiondicInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功!",
                    navTabId = "SafetyConstruction.ScmSafetyConstructionDic.SafetyConstructionDicList",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败!",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        public ActionResult SafetyConstructionDicEdit(string id)
        {
            ScmSafetyconstructiondicViewModel model = new ScmSafetyconstructiondicViewModel();
            model.TheSafetyconstructiondicInfo = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicByID(new Guid(id));
            return View(model);
        }
        public JsonResult SafetyConstructionDicEditData(ScmSafetyconstructiondicViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                ScmSafetyconstructiondicUIRule.UpdateEntity(vm.TheSafetyconstructiondicInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功!",
                    navTabId = "SafetyConstruction.ScmSafetyConstructionDic.SafetyConstructionDicList",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败!",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        public JsonResult SafetyConstructionDicDelete(string ids)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                ScmSafetyconstructiondicUIRule.DeleteEntityByMultiStringID(ids);
            });
            dynamic jsonReturn = new ExpandoObject();
            //删除成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功!",
                    navTabId = "SafetyConstruction.ScmSafetyConstructionDic.SafetyConstructionDicList",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败!",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 获取字典信息Json
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDicJson()
        {
            string url = Request.QueryString["url"];
            string pids = Request.QueryString["pids"]; ;
            List<treeNodes> treenodes = new List<treeNodes>();
            foreach (string pid in pids.Split(','))
            {
                List<ScmSafetyconstructiondicInfo> list = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicByPID(pid);
                if (list.Count > 0)
                {
                    ScmSafetyconstructiondicInfo pInfo = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicByID(new Guid(pid));
                    treeNodes pnode = new treeNodes();
                    pnode.id = pInfo.Scd_ID;
                    pnode.pId = "0";
                    pnode.name = pInfo.Scd_Name;
                    treenodes.Add(pnode);
                    foreach (ScmSafetyconstructiondicInfo info in list)
                    {
                        if (info.Scd_Name != "其它人员")
                        {
                            treeNodes node = new treeNodes();
                            node.id = info.Scd_ID;
                            node.pId = pInfo.Scd_ID;
                            node.name = info.Scd_Name;
                            node.target = "ajax";
                            node.url = url + "?Scd_ID=" + info.Scd_ID;
                            treenodes.Add(node);
                        }
                    }
                }
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
    }
}